package com.wise.service.cms.content.category;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.fasterxml.jackson.databind.BeanProperty;
import com.wise.entities.content.Menu;
import com.wise.entities.content.ProductCategory;
import com.wise.framework.basic.DateTimeUtil;
import com.wise.framework.dao.SqlSelect;
import com.wise.framework.daomodel.DaoList;
import com.wise.framework.daomodel.DaoOne4Update;
import com.wise.framework.daomodel.DaoSave;
import com.wise.framework.session.CurrentUserId;
import com.wise.model.content.ExtContent;
import com.wise.summer.v1.cms.content.category.UpdateCategoryById;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.summerframework.model.SummerService;
import org.summerframework.model.SummerServiceBean;

import java.util.List;
import java.util.stream.Collectors;

import static com.wise.model.content.ContentErrMsgs.Err_001000250007;
import static com.wise.model.content.ContentErrMsgs.Err_001000250042;

/**
 * @author shaoming
 * @date 2019-09-10
 */
@Service
@SummerService
public class UpdateCategoryByIdService implements SummerServiceBean<UpdateCategoryById> {

    @Override
    public void sum(UpdateCategoryById summer) {
        if (summer.getType() == null) {
            summer.setType("2");
        }
        ProductCategory productCategory = DaoOne4Update.s(ProductCategory.class, summer.getId(), Err_001000250007);
        BeanUtil.copyProperties(summer, productCategory);
        productCategory.setLastUpdateBy(CurrentUserId.s(true));
        productCategory.setLastUpdateDate(DateTimeUtil.timestampNow());
        DaoSave.s(productCategory);
        updateCategoryInfo(productCategory, summer.getCategoryType().toString());
        summer.setSummerResult(productCategory);
    }


    /**
     * 同步刷产品类别信息
     *
     * @param model
     */
    private void updateCategoryInfo(ProductCategory model, String type) {
        SqlSelect selectMenu = SqlSelect.selec("*").fromClassName(Menu.class)
                .like(Menu._extContent, "%" + model.getId() + "%");
        List<Menu> menus = DaoList.s(Menu.class, selectMenu);
        if (menus.isEmpty()) {
            return;
        }
        for (Menu menu : menus) {
            if (menu.getExtContent() == null || menu.getExtContent().isEmpty() && menu.getExtContent().equals("[]")) {
                continue;
            }
            List<ExtContent> extContents = JSONArray.parseArray(menu.getExtContent(), ExtContent.class);
            extContents.stream().filter(f -> type.equals(f.getType()))
                    .forEach(f -> f.getInfo().stream().filter(info -> model.getId().equals(info.getId())).forEach(info1 -> BeanUtils.copyProperties(model, info1)));
            Menu menu1 = DaoOne4Update.s(Menu.class, menu.getId(), Err_001000250042);
            menu1.setExtContent(JSON.toJSONString(extContents));
            DaoSave.s(menu1);
        }
    }

    /**
     * 当隐藏产品类别时，更改菜单信息
     *
     * @param model 产品类别
     */
    private void updateExtContent(ProductCategory model) {
        if (!model.isShowStatus()) {
            SqlSelect selectMenu = SqlSelect.selec("*").fromClassName(Menu.class);
            List<Menu> contents = DaoList.s(Menu.class, selectMenu);
            if (!contents.isEmpty()) {
                for (Menu content : contents) {
                    if (!content.getExtContent().isEmpty() && !content.getExtContent().equals("[]")) {
                        JSONArray ext = JSON.parseArray(content.getExtContent());
                        List<ExtContent> extContents = JSONArray.parseArray(ext.toJSONString(), ExtContent.class);
                        if (!extContents.isEmpty()) {
                            if (extContents.get(0).getInfo() != null) {
                                for (ProductCategory productCategory : extContents.get(0).getInfo()) {
                                    if (productCategory.getId().equals(model.getId())) {
                                        extContents.get(0).getInfo().remove(productCategory);
                                        Menu menu = DaoOne4Update.s(Menu.class, content.getId(), Err_001000250042);
                                        menu.setExtContent(JSON.toJSONString(extContents));
                                        DaoSave.s(menu);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

}
